package com.bm.specialvehiclefordispatcher.activity.setting;

import android.annotation.SuppressLint;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import cn.jpush.android.api.JPushInterface;

import com.android.volley.VolleyError;
import com.bm.corelibs.AppManager;
import com.bm.corelibs.logic.BaseLogic.NListener;
import com.bm.corelibs.utils.SharedPreferencesUtil;
import com.bm.corelibs.utils.ToastMgr;
import com.bm.specialvehiclefordispatcher.R;
import com.bm.specialvehiclefordispatcher.Tools.UpdateVersionBroadCast;
import com.bm.specialvehiclefordispatcher.activity.BaseActivity;
import com.bm.specialvehiclefordispatcher.activity.WebActivity;
import com.bm.specialvehiclefordispatcher.activity.login.LoginActivity;
import com.bm.specialvehiclefordispatcher.bean.BaseData;
import com.bm.specialvehiclefordispatcher.constants.Constant;
import com.bm.specialvehiclefordispatcher.logics.LoginManager;
import com.bm.specialvehiclefordispatcher.logics.SettingManager;
import com.bm.specialvehiclefordispatcher.views.CommonNavigationBar;
import com.bm.specialvehiclefordispatcher.views.CustomDialog;

public class SettingActivity extends BaseActivity implements OnClickListener {
	CommonNavigationBar bar;
	RelativeLayout kefuzixun;
	RelativeLayout xinbanben;
	RelativeLayout zhinan;
	RelativeLayout fankui;
	RelativeLayout guanyu;
	Button tuichu;
	CustomDialog commonDialog;
	SettingManager manager = new SettingManager();
	SharedPreferencesUtil sp;
	CustomDialog customDialog;
	String url;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		findViews();
		init();
		addListeners();
	}

	@Override
	public void findViews() {
		bar = (CommonNavigationBar) findViewById(R.id.navi);
		bar.setListener(null);
		bar.setTitle("设置");
		kefuzixun = (RelativeLayout) findViewById(R.id.kefuzixun);
		xinbanben = (RelativeLayout) findViewById(R.id.xinbanben);
		zhinan = (RelativeLayout) findViewById(R.id.zhinan);
		fankui = (RelativeLayout) findViewById(R.id.fankui);
		guanyu = (RelativeLayout) findViewById(R.id.guanyu);
		tuichu = (Button) findViewById(R.id.tuichu);
	}

	@Override
	public void init() {
		commonDialog = new CustomDialog(SettingActivity.this);
		sp=new SharedPreferencesUtil(mActivity, Constant.SpName);
	}

	@Override
	public void addListeners() {
		kefuzixun.setOnClickListener(this);
		xinbanben.setOnClickListener(this);
		zhinan.setOnClickListener(this);
		fankui.setOnClickListener(this);
		guanyu.setOnClickListener(this);
		tuichu.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
		case R.id.kefuzixun:
			intent = new Intent(mActivity, HotlineCounselingActivity.class);
			startActivity(intent);
			break;
		case R.id.xinbanben:
			loadingDialog.show();
			manager.upDataVersion(mActivity, new NListener<BaseData>() {

				@Override
				public void onResponse(BaseData data) {
					loadingDialog.dismiss();
					if (data.status==Constant.STATUS_OK) {
						commonDialog.show();
						url=data.data.versionUrl;
						commonDialog.showTitleAndMsg("提示", "发现最新版本,是否更新",
								new OnClickListener() {

									@Override
									public void onClick(View v) {

										commonDialog.dismiss();
										startAppUpdate(url);
									}
								});
					}else {
						commonDialog.show();
						commonDialog.showTitleAndMsg("提示", "当前版本已为最新",
								new OnClickListener() {

									@Override
									public void onClick(View v) {

										commonDialog.dismiss();
									}
								});
					}
				}

				@Override
				public void onErrResponse(VolleyError error) {
					loadingDialog.dismiss();
				}

				@Override
				public void onAllPageLoaded(int nowPage, int totalPage) {
					
				}
			});
			
			
			
			
			break;
		case R.id.zhinan:
			loadingDialog.show();
			manager.getSinglePage("站点端使用指南", getListener());
			break;
		case R.id.fankui:
			intent = new Intent(mActivity, FeedbackActivity.class);
			startActivity(intent);

			break;
		case R.id.guanyu:
			loadingDialog.show();
			manager.getSinglePage("关于平台", getListener());
			break;
		// 退出登录
		case R.id.tuichu:

			customDialog = new CustomDialog(mActivity);
			customDialog.show();
			customDialog.showTitleAndMsg("提示", "是否确定注销账号?", new OnClickListener() {
				@Override public void onClick(View v) {
					loadingDialog.show();
					customDialog.dismiss();
					manager.logOff(sp.getStringByKey("id"), listener);
				}
			});

			break;

		default:
			break;
		}
	}
	
	private NListener<BaseData> getListener() {
		return new NListener<BaseData>() {
			@Override
			public void onResponse(BaseData data) {
				loadingDialog.dismiss();
				if (data.status == Constant.STATUS_OK) {
					if(data.data.singlePage != null) {
						Intent intent = new Intent(mActivity, WebActivity.class);
						intent.putExtra("title", data.data.singlePage.typeName);
						intent.putExtra("html", data.data.singlePage.content);
						startActivity(intent);
					}
				} else {
					ToastMgr.show(data.msg);
				}
			}

			@Override
			public void onErrResponse(VolleyError error) {
				loadingDialog.dismiss();
			}

			@Override
			public void onAllPageLoaded(int nowPage, int totalPage) {
			}
		};
	}
	
	NListener<BaseData> listener=new NListener<BaseData>() {

		@Override
		public void onResponse(BaseData data) {
			loadingDialog.dismiss();
			if (data.status==Constant.STATUS_OK) {
				showToast("注销成功");
				LoginManager.getInstance(mActivity).clearUserInfo();
				AppManager.getAppManager().finishAllActivity();
				Intent intent = new Intent(mActivity,
						LoginActivity.class);
				startActivity(intent);
				sp.saveString("login", "no");
				JPushInterface.setAlias(mActivity, "", null);
			}else {
				showToast(data.msg);
			}
		}

		@Override
		public void onErrResponse(VolleyError error) {
			loadingDialog.dismiss();
			customDialog.dismiss();
		}

		@Override
		public void onAllPageLoaded(int nowPage, int totalPage) {
			
		}
	};
	
	@SuppressLint("NewApi")
	public void startAppUpdate(String appLoaderUrl) {

		try {
			String serviceString = Context.DOWNLOAD_SERVICE;
			DownloadManager downloadManager;
			downloadManager = (DownloadManager) getSystemService(serviceString);
			Uri uri = Uri.parse(appLoaderUrl);
			DownloadManager.Request request = new Request(uri);
			request.setTitle("砼车帮");// 设置标题
			request.setDestinationInExternalPublicDir("/Specialvehiclefordispatcher/download/",
					"Specialvehiclefordispatcher.apk");// 设置地址
			request.setDescription("全新版本,重磅归来");// 设置介绍信息
			// 设置允许的网络下载，默认在任何网络下都允许下载
			request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
					| DownloadManager.Request.NETWORK_MOBILE);
			request.allowScanningByMediaScanner();// 设置可被管理器找到
			downloadManager.enqueue(request);
			SettingActivity.this.registerReceiver(new UpdateVersionBroadCast(
					this), new IntentFilter(
					DownloadManager.ACTION_DOWNLOAD_COMPLETE));

		} catch (Exception e) {

			e.printStackTrace();
		}

	}


}
